package com.zeyi.chat.views;

import com.zeyi.chat.po.User;
import com.zeyi.chat.utils.AvatarUtil;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;

/**
 * @author zeyi
 */
public class ChatContentListItem {
    private AnchorPane pane;
    private ImageView avatar;
    private Label chatContent;
    private ImageView image;

    public ChatContentListItem(){
        pane = new AnchorPane();
        avatar = new ImageView();
        image = new ImageView();
        chatContent = new Label();
        avatar.setFitWidth(40);
        avatar.setFitHeight(40);
        avatar.setLayoutY(10);
        image.setLayoutY(25);
        image.setFitWidth(15);
        image.setFitHeight(15);
        chatContent.setLayoutY(10);
        chatContent.setPadding(new Insets(15, 15, 15, 15));
        chatContent.setWrapText(true);
        pane.setStyle("-fx-padding: 0 0 10px 0");
    }

    public Pane Me(String avatarUrl, String text, String type){
        AnchorPane.setRightAnchor(avatar, 20.0);
        avatar.setImage(new Image(AvatarUtil.suffix + avatarUrl));
        setCircleAvatar();
        AnchorPane.setRightAnchor(image, 60.0);
        image.setImage(new Image("/images/chatMe.png"));
        AnchorPane.setRightAnchor(chatContent, 72.0);
        chatContent.setStyle("-fx-background-radius: 15px;-fx-background-color: #AED9E7;-fx-text-fill: inherit;");
        chatContent.setText(text);
        if (type.equals("0")) {
            pane.setPrefWidth(695);
        } else {
            pane.setPrefWidth(515);
        }
        pane.getChildren().addAll(avatar, image, chatContent);
        return pane;
    }

    public Pane Other(User sender, String text, String type){//别人的消息
        avatar.setImage(new Image(AvatarUtil.suffix + sender.getAvatar()));
        setCircleAvatar();
        image.setImage(new Image("/images/chatOther.png"));
        chatContent.setStyle("-fx-background-radius: 15px;-fx-background-color: #91EF91;-fx-text-fill: inherit;");
        chatContent.setText(text);
        avatar.setLayoutX(20);
        image.setLayoutX(60);
        chatContent.setLayoutX(72);
        pane.setPrefWidth(515);
        pane.getChildren().addAll(avatar, image, chatContent);
        if (type.equals("1")) {
            Label label = new Label(sender.getUsername());
            label.setLayoutX(72);
            label.setLayoutY(8);
            label.setStyle("-fx-text-fill: #909399");
            image.setLayoutY(48);
            chatContent.setLayoutY(33);
            pane.setPrefWidth(515);
            pane.getChildren().add(label);
        } else {
            pane.setPrefWidth(695);
        }
        return pane;
    }

    private void setCircleAvatar() {
        Rectangle rectangle = new Rectangle(avatar.prefWidth(-1), avatar.prefHeight(-1));
        rectangle.setArcHeight(avatar.prefHeight(-1));
        rectangle.setArcWidth(avatar.prefWidth(-1));
        avatar.setClip(rectangle);
    }
}
